package com.ning.parking.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotDrivewayInfo;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.EditDrivewayDTO;
import com.ning.parking.domain.dto.ParkingLotBaseInfoDto;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotDrivewayInfoService;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenN
 * @Date 2025/3/7 15:32
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@RequestMapping("parkingLot")
public class DrivewayController {

    @Autowired
    private ParkingLotDrivewayInfoService parkingLotDrivewayInfoService;

    public boolean isValidIPv4(String ip) {
        if ("localhost".equals(ip)) {
            return true;
        }
        return ip.matches("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    }

    @PostMapping("saveParkingLotDrivewayInfo")
    public R<Long> saveDrvicewayInfo(@RequestBody List<ParkingLotDrivewayInfo> parkingLotDrivewayInfo) {

        for (ParkingLotDrivewayInfo lotDrivewayInfo : parkingLotDrivewayInfo) {
            if (lotDrivewayInfo.getParkingLotId() == null){
                throw new BizException("请绑定到停车场");
            }

            if (ObjectUtil.isNotEmpty(lotDrivewayInfo.getIp()) && !isValidIPv4(lotDrivewayInfo.getIp())) {
                throw new BizException("输入正确IP");
            }

            lotDrivewayInfo.setUpdateTime(DateUtils.date());
            lotDrivewayInfo.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
            lotDrivewayInfo.setCreateBy(GlobalVariableUtils.getUser().getUserName());
            lotDrivewayInfo.setCreateTime(DateUtils.date());
            lotDrivewayInfo.setParentParkingLotId(lotDrivewayInfo.getParkingLotId());
            lotDrivewayInfo.setDrivewayCode("TD"+DateUtils.date().getTime());
            boolean save = parkingLotDrivewayInfoService.save(lotDrivewayInfo);
            if (!save){
                /**
                 * -------------------
                 */
                parkingLotDrivewayInfoService.saveDrvicewayInfo(BeanUtil.toBean(lotDrivewayInfo, EditDrivewayDTO.class));
            }
        }

        return R.ok();
    }

    @GetMapping("get-driveway-info-by-parking-lot-id")
    public R<List<ParkingLotDrivewayInfo>> byId(Long parkingLotId) {
        if (parkingLotId == null){
            throw new BizException("ParkingLotId is null ");
        }
        return R.ok(parkingLotDrivewayInfoService.lambdaQuery().eq(ParkingLotDrivewayInfo::getParkingLotId,parkingLotId).list());
    }


    @PostMapping("updParkingLotDrivewayInfo")
    public R<Boolean> updParkingLotDrivewayInfo(@RequestBody List<ParkingLotDrivewayInfo> parkingLotDrivewayInfo) {
        for (ParkingLotDrivewayInfo lotDrivewayInfo : parkingLotDrivewayInfo) {
//            if (lotDrivewayInfo.getDrivewayId() == null){
//                throw new BizException("DrivewayId is null ");
//            }
            if (ObjectUtil.isNotEmpty(lotDrivewayInfo.getIp()) && !isValidIPv4(lotDrivewayInfo.getIp())) {
                throw new BizException("输入正确IP");
            }
            lotDrivewayInfo.setUpdateTime(DateUtils.date());
            lotDrivewayInfo.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
            boolean updateById = parkingLotDrivewayInfoService.updateById(lotDrivewayInfo);
        }

        return R.ok();
    }

}
